//
//  MapLocationSimple.m
//  carcareIOS
//
//  Created by ileo on 15/7/17.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "MapLocationSimple.h"
#import "LocAnnotationView.h"
#import "MapReGeocodeSimple.h"

@interface MapLocationSimple()

@property (nonatomic, strong) MapReGeocodeSimple *mapSimple;

@end

@implementation MapLocationSimple

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        self.mapView.delegate = self;
        self.mapView.zoomLevel = 15.5;
        self.mapSimple = [[MapReGeocodeSimple alloc] init];
        self.locImage = ImageNamed(@"loc");
        self.locCenterOffset = CGPointMake(0, 0);
    }
    return self;
}

-(void)setCoordinate:(CLLocationCoordinate2D)coordinate{
    
    if (_coordinate.latitude == coordinate.latitude && _coordinate.longitude == coordinate.longitude && coordinate.latitude == 0) {
        return;
    }

    _coordinate = coordinate;

    __weak __typeof(self) wself = self;
    [self.mapSimple reGeoCodeCoordinate:coordinate finish:^(NSString *poi, AMapReGeocode *reGeocode) {
        for (id<MAAnnotation> ann in wself.mapView.annotations) {
            if ([ann isKindOfClass:[LocAnnotation class]]) {
                [wself.mapView removeAnnotation:ann];
                break;
            }
        }
        LocAnnotation *annotation = [[LocAnnotation alloc] init];
        annotation.coordinate = coordinate;
        annotation.title = poi;
        if (reGeocode.pois.count > 0) {
            for (int i = 0; i < reGeocode.pois.count; i++) {
                AMapPOI *poi = reGeocode.pois[i];
                if ([poi.address isNotNil]) {
                    NSString *path = [poi.name componentsSeparatedByString:@"("][0];
                    annotation.title = path;
                    annotation.subtitle = poi.address;
                    break;
                }
            }
        }
        [wself.mapView addAnnotation:annotation];
    } error:nil];
    
}

-(void)showLocation{
    if (self.coordinate.latitude == 0) {
        return;
    }
    [self.mapView setCenterCoordinate:self.coordinate animated:YES];
}

-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    if ([annotation isKindOfClass:[LocAnnotation class]]) {
        LocAnnotationView *annotationView = [[LocAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"carAnnotation"];
        annotationView.CalloutViewUpdate = self.CalloutViewUpdate;
        annotationView.calloutView = self.calloutView;
        annotationView.image = self.locImage;
        annotationView.centerOffset = self.locCenterOffset;
        annotationView.annotation = annotation;
        annotationView.canShowCallout = NO;
        annotationView.selected = YES;
        return annotationView;
    }
    return nil;
}
- (void)mapView:(MAMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    if(self.mapDidTouch) self.mapDidTouch();
}
- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    if(self.mapDidTouch) self.mapDidTouch();
}

@end
